home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / ingres04.lzh / source / dbu / save.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-20  |  4.2 KB  |  241 lines

  1. # include    <pv.h>
  2. # include    <ingres.h>
  3. # include    <aux.h>
  4. # include     <func.h>
  5. # include    <opsys.h>
  6. # include    <errors.h>
  7. #ifdef linux
  8. # include    <time.h>
  9. #else
  10. extern int timezone; 
  11. #endif
  12.  
  13. extern    short    tTdbu[];
  14. extern    int    save();
  15. extern    int    null_fn();
  16.  
  17. struct fn_def SaveFn =
  18. {
  19.     "SAVE",
  20.     save,
  21.     null_fn,        /* initialization function */
  22.     null_fn,
  23.     NULL,
  24.     0,
  25.     tTdbu,
  26.     100,
  27.     'Z',
  28.     0
  29. };
  30.  
  31. /*
  32. **  SAVE RELATION UNTIL DATE
  33. **
  34. **    This function arranges to save a named relation until a
  35. **    specified date.
  36. **
  37. **    Parameters:        (pv_type is PV_STR for all of them)
  38. **    0 -- relation name
  39. **    1 -- month (1 -> 12 or "jan" -> "dec" or a variety of other codes)
  40. **    2 -- day (1 -> 31)
  41. **    3 -- year (1970 -> ?)
  42. **
  43. **    Uses trace flag 44
  44. **    Uses error messages 56xx
  45. */
  46.  
  47. int    DmSize[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  48.  
  49. #ifdef linux
  50. int dysize(int year)
  51. {
  52.     if (year % 400 == 0)
  53.         return 366;
  54.     if (year % 4 == 0 && year % 100 != 0)
  55.         return 366;
  56.     return 365;
  57. }
  58. #endif
  59.  
  60. save(parmc, parmv)
  61. int    parmc;
  62. PARM    parmv[];
  63. {
  64.     long        date;
  65.     register int    i;
  66.     extern DESC    Reldes;
  67.     TID        tid;
  68.     extern char    *Usercode;
  69.     struct relation    relk, relt;
  70.     int        day, month, year;
  71.  
  72.     /* convert date */
  73.     /* "date" will be # of days from 1970 for a while */
  74.     date = 0;
  75.  
  76.     if (parmc == 1)
  77.         goto nodate;
  78.  
  79.     /*
  80.     **  Validate parameters.
  81.     **
  82.     **    According to my pocket calculator, a 31 bit number will
  83.     **    hold 70 years of accuracy -- hence the 2035 cutoff.  If
  84.     **    this code is still around in 2035, I apologize in
  85.     **    advance.
  86.     */
  87.  
  88.     year = atoi(parmv[3].pv_val.pv_str);
  89.     if (year < 1970 || year > 2035)
  90.         return (error(BADYEAR, parmv[3].pv_val.pv_str, 0));    /* bad year */
  91.     if (monthcheck(parmv[1].pv_val.pv_str, &month))
  92.         return (error(BADMONTH, parmv[1].pv_val.pv_str, 0));    /* bad month */
  93.     day = atoi(parmv[2].pv_val.pv_str);
  94.     if (day < 1 || day > monthsize(--month, year))
  95.         return (error(BADDAY, parmv[2].pv_val.pv_str, 0));    /* bad day */
  96.  
  97.     /* do year conversion */
  98.     for (i = 1970; i < year; i++)
  99.     {
  100.         date += dysize(i);
  101.     }
  102.  
  103.     /* do month conversion */
  104.     for (i = 0; i < month; i++)
  105.         date += DmSize[i];
  106.     /* once again, allow for leapyears */
  107.     if (month >= 2 && year % 4 == 0 && year % 100 != 0)
  108.         date += 1;
  109.  
  110.     /* do day conversion */
  111.     date += day - 1;
  112.  
  113.     /* we now convert date to be the # of hours since 1970 */
  114.     date *= 24;
  115.  
  116.     /* do daylight savings computations */
  117.     /*  <<<<< none now >>>>> */
  118.  
  119.     /* convert to seconds */
  120.     date *= 60 * 60;
  121.  
  122.     /* adjust to local time */
  123.     date += timezone;
  124.  
  125. #    ifdef xZTR1
  126.     if (tTf(45, 1))
  127.         printf("%s", ctime(&date));
  128. #    endif
  129.  
  130. nodate:
  131.     /* let's check and see if the relation exists */
  132.     opencatalog("relation", OR_WRITE);
  133.     clearkeys(&Reldes);
  134.     setkey(&Reldes, &relk, parmv[0].pv_val.pv_str, RELID);
  135.     setkey(&Reldes, &relk, Usercode, RELOWNER);
  136.     if (getequal(&Reldes, &relk, &relt, &tid))
  137.     {
  138.         return (error(RELNOTFOUND, parmv[0].pv_val.pv_str, 0));    /* relation not found */
  139.     }
  140.  
  141.     /* check that it is not a system catalog */
  142.     if (relt.relstat & S_CATALOG)
  143.         return (error(NOSAVESYSREL, parmv[0].pv_val.pv_str, 0));    /* cannot save sys rel */
  144.     /* got it; lets change the date */
  145.     relt.relsave = date;
  146.  
  147. #    ifdef xZTR2
  148.     if (tTf(45, 2))
  149.     {
  150.         printup(&Reldes, &relt);
  151.     }
  152. #    endif
  153.  
  154.     if ((i = replace(&Reldes, &tid, &relt, 0)) < 0)
  155.         syserr("SAVE: replace %d", i);
  156.  
  157.     /* that's all folks.... */
  158.     return (0);
  159. }
  160. /*
  161. **  MONTHCHECK
  162. */
  163.  
  164. struct monthtab
  165. {
  166.     char    *code;
  167.     int    month;
  168. };
  169.  
  170. struct monthtab    Monthtab[] =
  171. {
  172.     "jan",        1,
  173.     "feb",        2,
  174.     "mar",        3,
  175.     "apr",        4,
  176.     "may",        5,
  177.     "jun",        6,
  178.     "jul",        7,
  179.     "aug",        8,
  180.     "sep",        9,
  181.     "oct",        10,
  182.     "nov",        11,
  183.     "dec",        12,
  184.     "january",    1,
  185.     "february",    2,
  186.     "march",    3,
  187.     "april",    4,
  188.     "june",        6,
  189.     "july",        7,
  190.     "august",    8,
  191.     "september",    9,
  192.     "october",    10,
  193.     "november",    11,
  194.     "december",    12,
  195.     0
  196. };
  197.  
  198. monthcheck(input, output)
  199. char    *input;
  200. int    *output;
  201. {
  202.     struct monthtab    *p;
  203.     int        month;
  204.  
  205.     /* month can be an integer, or an alphanumeric code */
  206.     month = atoi(input);
  207.     if (month != 0)
  208.     {
  209.         *output = month;
  210.         return (month < 1 || month > 12);
  211.     }
  212.     for (p = Monthtab; p->code; p++)
  213.     {
  214.         if (sequal(input, p->code))
  215.         {
  216.             *output = p->month;
  217.             return (0);
  218.         }
  219.     }
  220.     return (1);
  221. }
  222.  
  223. /*
  224. **  MONTHSIZE -- determine size of a particular month
  225. */
  226.  
  227. monthsize(month, year)
  228. int    month;
  229. int    year;
  230. {
  231.     register int    size;
  232.  
  233.     size = DmSize[month];
  234.     if (month == 1 && dysize(year) == 366)
  235.         /* This is February of a leap year */
  236.         size++;
  237.  
  238.     return (size);
  239.  
  240. }
  241.